<template>
  <div>
    <zz-tree
      :treeData="projectList"
      :options="projectOptions"
      @node-select="itemSelect"
      @node-click="itemClick"
    ></zz-tree>
  </div>
</template>

<script>
  export default {
    name: 'zz-constructProject',
    data () {
      return {
        editName: '',
        projectOptions: {
          halfCheckedStatus: true, // 控制父框是否需要半钩状态
          lazy: true,
          load: this.loadingChild
        },
        visible: false,
        projectList: [],
        projectPerson: [],
        projectTeam: {},
        organizeId: '',
        teamId: '',
        parentId: '',
        moveType: 1,
        type: 2 // 项目部2 参建各方5
      }
    },
    props: {
      // 是否显示操作栏
      showSelect: {
        type: Boolean,
        default: false
      },
      // 是否显示所有项目列表
      isAllProject: {
        type: Boolean,
        default: false
      },
      // 是否显示项目成员
      isProjectOrganize: {
        type: Boolean,
        default: true
      },
      // 是否显示参建各方
      isProjectTeam: {
        type: Boolean,
        default: true
      },
      pjId: {
        type: Number,
        default: 0
      },
      projectName: {
        type: String,
        default: '项目部'
      }
    },
    created () {
      this.ready()
    },
    methods: {
      ready () {
        if (this.isAllProject) {
          this.getProjectList()
        } else {
          this.getProjectLabel()
        }
      },
      // 获取项目列表
      getProjectList () {
        this.$axios({
          method: 'get',
          url: `${this.$site.projectServer}/front/person/findConstructProject.htm`,
          data: this.$qs.stringify({'query.authCheck': 1}, {indices: false})
        }).then(res => {
          const {list} = res.data
          this.projectList = list.map((item, index) => {
            if (index === 0) {
              this.pjId = item.pjId
              item.open = true
            } else {
              item.open = false
            }
            item.open = true
            item.label = item.title
            item.notAllowed = true
            item.children = [
              {
                label: '项目部',
                pjId: item.pjId,
                children: [],
                isProjectOrganizeList: true,
                isProjectOrganize: true},
              {
                label: '参建各方',
                pjId: item.pjId,
                children: [],
                isProjectTeamList: true,
                isProjectTeam: true}
            ]
            item.children.forEach(child => {
              if (this.showSelect) {
                this.$set(child, 'showSelect', false)
                this.$set(child, 'select', {1: '新增'})
              }
            })
            return item
          })
          this.$emit('projectOrganize', {pjId: this.pjId})
        })
      },
      getProjectLabel () {
        this.projectList = []
        const item = {}
        item.open = true
        item.label = this.projectName
        item.notAllowed = true
        item.children = []
        if (this.isProjectTeam) {
          item.children.push({
            label: '甲方项目管理人员',
            pjId: this.pjId,
            source: 1,
            checked: true,
            closeLeaf: true,
            isProjectTeamList: true,
            isProjectTeam: true})
        }
        if (this.isProjectOrganize) {
          const data = {
            'query.type': 'stage',
            'query.projectId': this.pjId
          }
          this.$axios({
            method: 'post',
            url: `${this.$site.projectServer}/front/person/findSectionList.htm`,
            data: this.$qs.stringify(data, {indices: false})
          }).then(res => {
            if (res.data.errorMsg) {
              this.$message.error(res.data.errorMsg)
            } else {
              res.data.list.forEach((x) => {
                let obj = {}
                Object.assign(obj, {label: x.stageName})
                Object.assign(obj, {pjId: this.pjId})
                Object.assign(obj, {stageId: x.stageId})
                Object.assign(obj, {children: []})
                this.$set(obj, 'open', false)
                this.$set(obj, 'checked', false)
                this.$set(obj, 'isStage', true)
                this.$set(obj, 'isProjectOrganize', true)
                this.$set(obj, 'isProjectOrganizeList', true)
                item.children.push(obj)
              })
            }
          })
        }
        this.$emit('ProjectTeam', item.children[0])
        this.projectList.push(item)
      },
      // 获取项目部
      async findProjectOrganizeList (node) {
        let data = await this.findProjectOrganizeListAwait(node)
        let tem = this.getParentNode(node, this.projectList)
        this.$set(tem, 'children', data)
      },
      findProjectOrganizeListAwait (node) {
        return new Promise((resolve) => {
          const data = {
            'query.type': 'section',
            'query.projectId': this.pjId,
            'query.stageId': node.stageId
          }
          this.$axios({
            method: 'post',
            url: `${this.$site.projectServer}/front/person/findSectionList.htm`,
            data: this.$qs.stringify(data, {indices: false})
          }).then(res => {
            if (res.data.errorMsg) {
              this.$message.error(res.data.errorMsg)
            } else {
              const arr = []
              const {list} = res.data
              list.forEach((x) => {
                let obj = {}
                Object.assign(obj, {label: x.section_name})
                Object.assign(obj, {pjId: node.pjId})
                Object.assign(obj, {section_id: x.section_id})
                Object.assign(obj, {stageId: node.stageId})
                Object.assign(obj, {children: []})
                this.$set(obj, 'open', false)
                this.$set(obj, 'checked', false)
                this.$set(obj, 'isProjectOrganize', true)
                this.$set(obj, 'isSection', true)
                this.$set(obj, 'isProjectOrganizeListChild', true)
                arr.push(obj)
              })
//              this.projectPerson = res.data.list
//              const unallocated = {
//                label: '未分配',
//                closeLeaf: true,
//                unallocated: true,
//                isProjectOrganize: true,
//                isProjectOrganizeListChild: true
//              }
//              this.$set(unallocated, 'checked', false)
//              arr.push(unallocated)
              resolve(arr)
            }
          })
        })
      },
      // 获取项目部下的子部门
      async findProjectOrganizeListChild (node) {
        let data = await new Promise((resolve) => {
          const arr = []
          arr.push(
            {
              label: '施工项目成员',
              pjId: node.pjId,
              section_id: node.section_id,
              stageId: node.stageId,
              source: 2,
              checked: false,
              closeLeaf: true,
              isProjectOrganize: true,
              isProjectOrganizeListChild: true},
            {
              label: '监理工程师',
              pjId: node.pjId,
              section_id: node.section_id,
              stageId: node.stageId,
              checked: false,
              source: 3,
              closeLeaf: true,
              isProjectOrganize: true,
              isProjectOrganizeListChild: true}
            )
          resolve(arr)
        })
        let tem = this.getParentNode(node, this.projectList)
        this.$set(tem, 'children', data)
      },
      // 获取参建各方
      async findProjectTeamList (node) {
        let data = await this.findProjectTeamListAwait(node)
        let tem = this.getParentNode(node, this.projectList)
        this.$set(tem, 'children', data)
      },
      findProjectTeamListAwait (node) {
        return new Promise(resolve => {
          const data = {
            'query.project_id': node.pjId
          }
          this.$axios({
            method: 'post',
            url: `${this.$site.projectServer}/front/person/findProjectTeamList.htm`,
            data: this.$qs.stringify(data, {indices: false})
          }).then(res => {
            if (res.data.errorMsg) {
              this.$message.error(res.data.errorMsg)
            } else {
              const arr = []
              res.data.list.forEach(item => {
                if (!item.parentId) {
                  Object.assign(item, {isProjectTeamListChild: true})
                  Object.assign(item, {isProjectTeam: true})
                  this.setTreeData(item)
                  arr.push(item)
                }
              })
              this.projectTeam = res.data.list
              const unallocated = {
                label: '未分配',
                closeLeaf: true,
                unallocated: true,
                isProjectTeam: true,
                isProjectTeamListChild: true
              }
              this.$set(unallocated, 'checked', false)
              arr.push(unallocated)
              resolve(arr)
            }
          })
        })
      },
      // 获取参建各方下的成员
      async findProjectTeamListChild (node) {
        let data = await new Promise((resolve) => {
          const arr = []
          this.projectTeam.forEach(item => {
            if (item.parentId === node.organizeId) {
              Object.assign(item, {isProjectTeamListChild: true})
              Object.assign(item, {isProjectTeam: true})
              this.setTreeData(item)
              arr.push(item)
            }
          })
          resolve(arr)
        })
        let tem = this.getParentNode(node, this.projectList)
        this.$set(tem, 'children', data)
      },
      async loadingChild (node) {
        if (node.isProjectOrganizeList) {
          this.findProjectOrganizeList(node)
        }
        if (node.isProjectOrganizeListChild) {
          this.findProjectOrganizeListChild(node)
        }
        if (node.isProjectTeamList) {
          this.findProjectTeamList(node)
        }
        if (node.isProjectTeamListChild) {
          this.findProjectTeamListChild(node)
        }
      },
      getParentNode (node, treeData) {
        let tem
        let postions = node.key.split('-')
        for (let [index, item] of postions.entries()) {
          switch (index) {
            case 0:
              break
            case 1:
              tem = treeData[item]
              break
            default:
              tem = tem.children[item]
          }
        }
        return tem
      },
      setTreeData (x) {
        Object.assign(x, {label: x.name})
        Object.assign(x, {children: []})
        this.$set(x, 'open', false)
        this.$set(x, 'checked', false)
        if (this.showSelect) {
          this.$set(x, 'showSelect', this.showSelect)
          Object.assign(x, {select: {1: '新增', 2: '重命名', 3: '删除', 4: '上移', 5: '下移'}})
        }
      },
      itemClick (node) {
        // 项目回调事件
        if (node.isProjectOrganizeList) {
          this.$emit('projectOrganize', node)
        }
        // 项目部回调事件
        if (node.isProjectOrganizeListChild) {
          this.$emit('projectOrganizeChild', node)
        }
        if (node.isProjectTeamList) {
          this.$emit('ProjectTeam', node)
        }
        if (node.isProjectTeamListChild) {
          this.$emit('ProjectTeamChild', node)
        }
      },
      itemSelect (option) {
        if (option.value.isProjectOrganize) {
          this.type = 2
          this.organizeId = option.value.organizeId
          this.teamId = null
        } else {
          this.type = 5
          this.organizeId = null
          this.teamId = option.value.teamId
        }
        this.pjId = option.value.pjId
        switch (Number.parseInt(option.key)) {
          // 新增
          case 1:
            this.parentId = option.value.organizeId
            this.editName = ''
            this.visible = true
            break
          // 重命名
          case 2:
            this.parentId = option.value.organizeId
            this.editName = option.value.name
            this.visible = true
            break
          // 删除
          case 3:
            this.del(option.value)
            break
          // 上移
          case 4:
            this.moveType = 1
            this.move()
            break
          // 下移
          case 5:
            this.moveType = -1
            this.move()
            break
        }
      },
      move () {
        let data = {}
        let url = ''
        if (this.type === 2) {
          data = {
            'form.organize_id': this.organizeId,
            'form.moveType': this.moveType,
            'form.project_id': this.pjId
          }
          url = `${this.$site.projectServer}/front/person/moveOrganization.htm`
        } else {
          data = {
            'form.teamId': this.teamId,
            'form.moveType': this.moveType,
            'form.project_id': this.pjId
          }
          url = `${this.$site.projectServer}/front/person/moveTeam.htm`
        }
        this.$axios({
          method: 'post',
          url: url,
          data: this.$qs.stringify(data, {indices: false})
        }).then(res => {
          if (res.data.errorMsg) {
            this.$message.error(res.data.errorMsg)
          } else {
            this.$message.success('操作成功!')
            this.ready()
          }
        })
      },
      del (node) {
        this.$MessageBox({
          showClose: true,
          message: '你确定要删除吗？',
          type: 'info',
          confirmHanler: () => {
            const data = {
              'form.organize_id': node.organizeId,
              'form.type': this.type,
              'form.project_id': node.pjId
            }
            this.$axios({
              method: 'post',
              url: `${this.$site.projectServer}/front/person/deleteOrganization.htm`,
              data: this.$qs.stringify(data)
            }).then(res => {
              if (res.data.errorMsg) {
                this.$message.error(res.data.errorMsg)
              } else {
                this.$message.success('操作成功!')
                this.ready()
              }
            })
          }
        })
      }
    }
  }
</script>
